<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '添加' : '编辑'"
      :visible.sync="info.isShow"
      width="30%"
      @close="close"
    >
      <div>{{ menuInfo }}</div>
      <el-form ref="form" :model="menuInfo" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="menuInfo.title"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select
            v-model="menuInfo.pid"
            placeholder="请选择活动区域"
            @change="changepid"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              :disabled="item.title == menuInfo.title"
              v-for="item in list"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型">
          <el-radio disabled v-model="menuInfo.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menuInfo.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单图标" v-if="menuInfo.type == 1">
          <el-select v-model="menuInfo.icon" placeholder="请选择活动区域">
            <el-option v-for="(item, index) in icons" :key="item" :value="item"
              ><i :class="item"></i
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 菜单出现地址 -->
        <el-form-item label="菜单地址" v-if="menuInfo.type == 2">
          <el-select v-model="menuInfo.url" placeholder="请选择菜单地址">
            <el-option
              v-for="item in indexRoute"
              :key="item.path"
              :label="item.meta.title"
              :value="'/' + item.path"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="menuInfo.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="add"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="update">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {indexRoute} from "../../../router/index"
import {reqmenuadd , reqmenuinfo ,reqmenuedit} from '../../../http/api'
import {successAlert} from "../../../utils/alert"
import {formClose} from '../../../mixins/index'
export default {
  props: ["info",'list'],
  mixins:[formClose],
  data() {
    return {
      menuInfo: {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      //图标合集
       icons: [
        "el-icon-goods",
        "el-icon-setting",
        "el-icon-s-help",
        "el-icon-upload",
        "el-icon-camera",
      ],
      //二级路由合集
      indexRoute

    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {},
  methods: {
    ...mapActions({}),
    //重置数据
    empty(){
      this.menuInfo= {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1, 
      }
    },
    //上级菜单改变时触发
    changepid(){
      if(this.menuInfo.pid == 0){
        this.menuInfo.type = 1
      }else{
        this.menuInfo.type = 2
      }
    },
    //处于编辑状态时获取信息
    async getOne(id){
      let {data : res} = await reqmenuinfo({id});
      if(res.code == 200){
        this.menuInfo = res.list;
        this.menuInfo.id = id;
      }
    },
    //添加信息
    async add(){
     console.log(this.menuInfo);
     let {data:res} = await reqmenuadd(this.menuInfo);
     if(res.code == 200){
       successAlert(res.msg);
       this.info.isShow = false;
       this.empty();
       this.$emit('init')
     }
    },
    //修改信息
    async update(){
      if(this.menuInfo.type == 1){
        this.menuInfo.url='';
      }else{
        this.menuInfo.icon = '';
      }
      let {data:res} = await reqmenuedit(this.menuInfo);
      if(res.code ==200){
        successAlert(res.msg);
        this.$emit('init');
        this.info.isShow=false
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>